//
//  ClassifyActivityController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/5/22.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "OrderedActivityModel.h"

#import "UserInfoModel.h"
#import "ClassifyActivityController.h"
#import "ActivityInfoViewController.h"
#import "OrderedActivityCell.h"

@interface ClassifyActivityController ()<UITableViewDelegate,UITableViewDataSource>

// 视图
@property (nonatomic,retain)UITableView *tableView;

// 数据
@property (nonatomic,retain)NSMutableArray *dataArray;          // 活动数据
@property (nonatomic,assign)NSInteger page;


@end

@implementation ClassifyActivityController

-(instancetype)init {
    if (self = [super init]) {
        self.title = @"相关活动";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化tableView
    [self setupTableView];
}

-(void)setupTableView {
    // 设置TableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    // 3.网络获取数据
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 3.1 获取活动数据
        self.page = 0;
        [self updateActivityDatePage:self.page getCount:6];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取活动数据
        self.page++;
        [self updateActivityDatePage:self.page getCount:6];
    }];
}

#pragma mark - 获取网络数据

// 获取活动数据
-(void)updateActivityDatePage:(NSInteger)page getCount:(NSInteger)count {
    // 判断是否(下拉刷新)
    if (page == 0) {
        // 下拉刷新,则清空数组
        self.dataArray = [[NSMutableArray alloc] init];
    }
    
    // 初始化参数
    NSDictionary *paramters = @{@"sid": [UserInfoModel shareInstance].stu_id,@"token": [UserInfoModel shareInstance].token,@"iid": @(self.iid) , @"page": @(page), @"count": @(count)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString: @"SDSY/getItemActivities" parameters: paramters success:^(NSDictionary *respondDictionary) {
        // 1.储存数据
        for (NSDictionary *dic in respondDictionary[@"Activities"]) {
            OrderedActivityModel *model = [[OrderedActivityModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        
        // 2.检查是否正常加载
        if ([respondDictionary[@"Activities"] count] < count) {
            // 没有更多数据
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            // 正常结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
        [self updateFailed];
    }];
}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

#pragma mark - tableView回调函数

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[OrderedActivityCell class] contentViewWidth:SCREEN_WIDTH];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    // 取出模型
    OrderedActivityModel *model = self.dataArray[indexPath.row];
    
    // 显示活动详情页
    ActivityInfoViewController *activityInfoViewController = [[ActivityInfoViewController alloc] init];
    activityInfoViewController.activityID = model.activity_id;
    [self.navigationController pushViewController:activityInfoViewController animated:true];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderedActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell==nil) {
        cell = [[OrderedActivityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    }
    
    OrderedActivityModel *model = self.dataArray[indexPath.row];
    
    cell.model = model;
    return cell;
}

@end

